package org.databasefacade.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

/**
 * This class is used to access data values
 * that are being passed around as part of 
 * the database facade system.
 * 
 * @author Daniel
 *
 */
public class DataRecord {
	private ArrayList aliasedDataValueList = new ArrayList();
	private HashMap aliasedDataValueHash = new HashMap();
	
	
	/**
	 * Add the data value. The value will be
	 * alias with the column name if it is set 
	 * otherwise it is aliased with the class
	 * toString() method.
	 * 
	 * @param value
	 */
	public void add(DataValue value)
	{
		String alias = value.toString();
		if ( value.getColumnName() != null ) {
			alias = value.getColumnName();
		}
		add(alias, value);
	}
	
	/**
	 * Add the data value to the list with the
	 * passed alias name.
	 * 
	 * @param alias
	 * @param value
	 */
	public void add(String alias, DataValue value)
	{
		if ( !aliasedDataValueList.contains(alias) ) {
			aliasedDataValueList.add(alias);
		}
		aliasedDataValueHash.put(alias, value);
	}
	
	/**
	 * Remove the element from the list
	 * 
	 * @param i
	 */
	public void remove(int i)
	{
		String alias = (String) aliasedDataValueList.get(i);
		aliasedDataValueList.remove(i);
		aliasedDataValueHash.remove(alias);
	}
	
	/**
	 * Remove the element from the list 
	 * 
	 * @param alias
	 */
	public void remove(String alias)
	{ 
		for ( int i = 0; i < aliasedDataValueList.size(); i++ ) {
			String lAlias = (String) aliasedDataValueList.get(i);
			if ( alias.equals(lAlias) ) {
				remove(i);
				break;
			}
		}
	}
	
	/**
	 * Clear the entire list
	 */
	public void clear()
	{
		aliasedDataValueList.clear();
		aliasedDataValueHash.clear();
	}
	
	/**
	 * Get the data value
	 * 
	 * @param i
	 * @return
	 */
	public DataValue get(int i) {
		String alias = (String) aliasedDataValueList.get(i);
		return get(alias);
	}
		
	/**
	 * Get the data value by alias name
	 * 
	 * @param alias
	 * @return
	 */
	public DataValue get(String alias)
	{
		return (DataValue) aliasedDataValueHash.get(alias);
	}
	
	/**
	 * Get the size() of the list.
	 * 
	 * @return
	 */
	public int size()
	{
		return aliasedDataValueList.size();
	}
	
	/**
	 * Get all the aliases on the list.
	 * 
	 * @return
	 */
	public Iterator aliases()
	{
		return aliasedDataValueList.iterator();
	}
}
